/**
* ...
* @author Default
* @version 0.1
*/
package org.anivision {
	
	import com.hurlant.eval.ast.Void;
	import de.polygonal.ds.Collection;
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	import org.anivision.document.DocumentManager;
	import org.anivision.forms.*;
	import org.anivision.eval.Eval;
	import org.anivision.plugins.MeshPlugin;
	import org.anivision.plugins.ObjFormat;
	import org.anivision.plugins.PluginInfo;
	import org.aswing.*;

	/**
	 * AppMain
	 * @private
	 */
	public class AppMain extends org.aswing.JWindow {
		
		private var _mainform:MainForm;
		private var _eval:Eval;
		private var _config:XML;
		private var _pluginInfo:Array = new Array();
		private var _pluginDummy:Plugins = new Plugins();
		private var _documentManager:DocumentManager;
				
		public function AppMain(owner:DisplayObjectContainer) {
			super(owner, false);
			_eval = new Eval();
			_documentManager = new DocumentManager(this);

			AsWingManager.setRoot(this);
			
			_mainform = new MainForm(owner);
			setContentPane(_mainform);
			_mainform.init();
			
			App.init(this);
			loadConfig();

		}		

		private function loadConfig():void {
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest('config.xml');
			loader.addEventListener(Event.COMPLETE, __configLoad);
			loader.load(request);
		}
				
		private function __configLoad(event:Event):void {
			_config = new XML(event.target.data);
			
			loadPlugins();
		}

		private function loadPlugins():void
		{
			loadInternalPlugins();
			
			var plugins:XML = _config.child('plugins')[0];
			trace(plugins);
			
			for each(var p:XML in plugins.child('plugin'))
			{
				if ( p.@autoload ==  true )
				{
//					var loader:URLLoader = new URLLoader();
//					var request:URLRequest = new URLRequest(plugins.@path + '/' + p.@name + '/config.xml');
					
					// create plugin info class to auto load all its scripts and data
					var pluginInfo:PluginInfo = new PluginInfo(p, this._eval, plugins.@path + '/' + p.@name);
					//loader.addEventListener(Event.COMPLETE, pluginInfo.__onLoad);
//					loader.load(request);

					this.openFile(plugins.@path + '/' + p.@name + '/config.xml', pluginInfo.__onLoad);
				
					// store plugin information
					this._pluginInfo.push(pluginInfo);
				}
			}
		}

		public function openFile(path:String, onComplete:Function = null, onProgress:Function = null):void {
			// to be implemented by extender.
		}
		
		private function loadInternalPlugins(): void {
			Plugins.add(new ObjFormat());
			Plugins.add(new MeshPlugin());
		}
		public function get mainform():MainForm {
			return _mainform;
		}
		
		public function get appPath():String {
			return ''; /* to be extended */
		}
		
		public function get documentManager():DocumentManager {
			return _documentManager;
		}
		
	}
	
}
